/**
 *  ChildView.cpp
 *
 *  Copyright (C) 2008  David Andrs <pda@jasnapaka.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "StdAfx.h"
#include "mLasq.h"
#include "ChildView.h"
#include "helpers.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChildView

CChildView::CChildView() {
}

CChildView::~CChildView() {
}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
	//{{AFX_MSG_MAP(CChildView)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
		NULL, HBRUSH(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CChildView::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	// TODO: Add your message handler code here

	// Do not call CWnd::OnPaint() for painting messages
}


int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	CreateFonts();

	CRect rc;
	//
	m_ctlStationName.Create(_T("Unknown Station"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | SS_LEFT, rc, this, IDC_STATION_NAME);
	m_ctlStationName.SetFont(&m_fntTitle);

	m_ctlCoverArt.Create(_T(""), WS_CHILD | WS_VISIBLE | WS_BORDER | SS_BITMAP, rc, this, IDC_COVER_ART);

	m_ctlTrack.Create(_T("Unknown Track"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | SS_LEFT, rc, this, IDC_TRACK);
	m_ctlTrack.SetFont(&m_fntBold);
	m_ctlArtist.Create(_T("Unknow Artist"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | SS_LEFT, rc, this, IDC_ARTIST);
	m_ctlArtist.SetFont(&m_fntBold);
	m_ctlAlbum.Create(_T("Unknown Album"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | SS_LEFT, rc, this, IDC_ALBUM);
	m_ctlAlbum.SetFont(&m_fntBold);

	m_ctlVolume.Create(WS_CHILD | WS_VISIBLE | TBS_HORZ | TBS_BOTTOM, rc, this, IDC_VOLUME);
	m_ctlVolume.SetRange(0, 32, FALSE);
	for (int i = 0; i < 16; i++)
		m_ctlVolume.SetTic(i * 2);

	m_ctlProgress.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | PBS_SMOOTH, rc, this, IDC_PROGRESS);

	m_ctlStatus.Create(_T("Idle"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | SS_LEFT, rc, this, IDC_STATUS);
	m_ctlStatus.SetFont(&m_fntBase);

	return 0;
}

void CChildView::OnSize(UINT nType, int cx, int cy) {
	CWnd::OnSize(nType, cx, cy);

	struct CPos {
		UINT nID;
		CRect rcRect;

		CPos(UINT id, const CPoint &pt, const CSize &sz) {
			nID = id;
			rcRect = CRect(
				SCALEX(pt.x), SCALEY(pt.y),
				SCALEX(pt.x) + SCALEX(sz.cx), SCALEY(pt.y) + SCALEY(sz.cy));
		}
	};

	CPos rcs_port[] = {
		// IDC_COVER_ART
		CPos(IDC_STATION_NAME, CPoint(7, 10), CSize(cx - 14, 15)),
		CPos(IDC_COVER_ART,  CPoint(7, 30), CSize(90, 90)),
		CPos(IDC_TRACK,      CPoint(105, 30), CSize(cx - 112, 14)),
		CPos(IDC_ARTIST,     CPoint(105, 45), CSize(cx - 112, 14)),
		CPos(IDC_ALBUM,      CPoint(105, 60), CSize(cx - 112, 14)),

		CPos(IDC_VOLUME,   CPoint(cx / 3, cy - 80), CSize((2 * cx / 3) - 7, 27)),
		CPos(IDC_PROGRESS, CPoint(7, cy - 45), CSize(cx - 14, 20)),
		CPos(IDC_STATUS,   CPoint(7, cy - 19), CSize(cx - 14, 14)),
	};
	CPos rcs_wide[] = {
		CPos(IDC_STATION_NAME, CPoint(7, 10), CSize(cx - 14, 15)),
		CPos(IDC_COVER_ART,  CPoint(7, 30), CSize(90, 90)),
		CPos(IDC_TRACK,  CPoint(105, 30), CSize(cx - 112, 14)),
		CPos(IDC_ARTIST, CPoint(105, 45), CSize(cx - 112, 14)),
		CPos(IDC_ALBUM,  CPoint(105, 60), CSize(cx - 112, 14)),
		CPos(IDC_VOLUME,   CPoint(0, cy - 60), CSize(cx - 14, 10)),
		CPos(IDC_PROGRESS, CPoint(7, cy - 45), CSize(cx - 14, 20)),
		CPos(IDC_STATUS, CPoint(7, cy - 19), CSize(cx - 14, 14)),
	};

	CPos *rcs;
	if (InWideMode())
		rcs = rcs_wide;
	else
		rcs = rcs_port;

	for (int i = 0; i < countof(rcs_port); i++) {
		CWnd *pWnd = GetDlgItem(rcs[i].nID);
		pWnd->MoveWindow(&rcs[i].rcRect);
	}

}

void CChildView::CreateFonts() {
	HGDIOBJ hObj = ::GetStockObject(SYSTEM_FONT);
	LOGFONT lf;
	::GetObject(hObj, sizeof(LOGFONT), (LPVOID) &lf);
	lf.lfHeight = SCALEY(12) + 1;
	m_fntBase.CreateFontIndirect(&lf);

	lf.lfWeight = FW_BOLD;
	lf.lfHeight = SCALEY(12) + 1;
	m_fntBold.CreateFontIndirect(&lf);

	lf.lfWeight = FW_BOLD;
	lf.lfHeight = SCALEY(13) + 1;
	m_fntTitle.CreateFontIndirect(&lf);
}

void CChildView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar) {
	UINT nID = pScrollBar->GetDlgCtrlID();
	if (nID == IDC_VOLUME) {
		CSliderCtrl *slider = (CSliderCtrl *) pScrollBar;
		AfxGetMainWnd()->SendMessage(UWM_VOLUME_CHANGED, 0, slider->GetPos() * 0x800);
	}
}



